package com.rfs.thread;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @author renfushuai
 * @date 2022/12/23
 * 管道输入、输出
 */
public class Piped {
    public static void main(String[] args) throws Exception {
        PipedWriter out=new PipedWriter();
        PipedReader in = new PipedReader();
        out.connect(in);
        new Thread(()->{
            int receive=0;
            while (true) {
                try {
                    if (((receive = in.read()) != -1)) {
                        System.out.print((char) receive);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }).start();
        int receive=0;
        try {
            while ((receive = System.in.read()) != -1) {
                out.write(receive);
            }
        }finally {
            out.close();
        }
    }
}
